exports.searchApplicationTemplate = function(req,res){

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //用户应用模板详情
    var appTemplateData = [];

    //获取用户的应用模板信息
    function searchUserApplicationInfo(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url    : CORE_APIConfig.coreAPI.easydCloudMemberApplicationTemplateSearch.Request_Api_Url + "/cmat_cmaid/"+restfulParams.cmat_cmaid+"/cmat_uid/"+restfulParams.cmat_uid + "/cmat_is_delete/0",  
                method : 'POST'
            },
            function (data) {

                appTemplateData = data.result.data;
                
                cb();

            }
        )

    }

    //循环获取应用每个模板的详细
    function getApplicationTemplateDetail(cb){

        F.SerialEach(appTemplateData,function(item,index,callback){

            //获取模板信息
            function searchTemplateInfo(eachCb){

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url    : CORE_APIConfig.coreAPI.easydCloudTemplateSupermarketSearch.Request_Api_Url + "/cts_id/"+item.cmat_ctsid,  
                        method : 'POST'
                    },
                    function (data) {

                        item.templateInfo = data.result.data[0];
                        
                        eachCb();

                    }
                )
            }

            //获取模板所属的主类型
            function searchTemplateMainType(eachCb){

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url    : CORE_APIConfig.coreAPI.easydCloudApplicationProductSearch.Request_Api_Url + "/cpp_id/"+item.templateInfo.cts_capid,  
                        method : 'POST'
                    },
                    function (data) {

                        item.templateInfo.appProduct = data.result.data[0].cpp_name;
                        
                        eachCb();

                    }
                )
            }

            //获取容器信息
            function searchConatinerInfo(eachCb){

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url    : CORE_APIConfig.coreAPI.easydCloudMemberApplicationContainerSearch.Request_Api_Url + "/cmac_id/"+item.cmat_cmacid,  
                        method : 'POST'
                    },
                    function (data) {

                        item.templateContainerInfo = data.result.data[0];
                        
                        eachCb();

                    }
                )
            }

            //查看是否开启云部署，并获取容器的状态
            function searchContainerStatus(eachCb){

                //如果非云端部署则直接跳过
                if(item.cmat_is_cloud_service != '1'){
                    return callback();
                }

                //获取容器状态
                var containerName = item.templateContainerInfo.cmac_name;
                
                R.Ssh_exec_dockerCommand(`${DockerSH.GetContainerStatus} ${containerName}`,function(rs){
                    
                    if(rs.indexOf('running') != -1){

                        item.templateContainerInfo.status = 1;

                    }else{
                        
                        item.templateContainerInfo.status = 0;

                    }

                    eachCb();
                            
                })

            }

            F.Serial([
                searchTemplateInfo,
                searchTemplateMainType,
                searchConatinerInfo,
                searchContainerStatus
            ],() => {
                callback();
            })

        },cb)

    }


    F.Serial([
        searchUserApplicationInfo,
        getApplicationTemplateDetail
    ],() => {
        RES.successResponse(res,appTemplateData);
    })

}